import _ from "lodash"
import Asciidoctor from "@asciidoctor/core"
import dayjs from "dayjs"
import S from "string"
import { AdocInfo } from "../types"

const hljsExt = require("asciidoctor-highlight.js")
const katexExt = require("asciidoctor-katex")

const asciidoctor = Asciidoctor()
const registry = asciidoctor.Extensions.create()
hljsExt.register(registry)
katexExt.register(asciidoctor.Extensions, {
  katexOptions: { macros: { "\\RR": "\\mathbb{R}" } },
})

export default function parse(adocText: string): AdocInfo {
  const doc = asciidoctor.load(adocText, {
    extension_registry: registry,
    attributes: {
      icons: "font",
      "source-highlighter": "highlightjs-ext",
      stem: "latexmath",
    },
  })

  const revisionInfo = doc.getRevisionInfo()
  return {
    title: S(doc.getTitle()).decodeHTMLEntities().s,
    author: doc.getAttribute("author"),
    tags: (() => {
      const tag = doc.getAttribute("tag")
      return _.isEmpty(tag) ? [] : _.split(tag, ",")
    })(),
    version: revisionInfo.getNumber(),
    date: _.isEmpty(revisionInfo.getDate())
      ? new Date(0).valueOf() // FIXME: 先强行置为 1970 年
      : dayjs(revisionInfo.getDate()).valueOf(),
    content: doc.convert(),
  }
}
